/*
 * @Author: MiNine
 * @Date: 2020-09-21 12:21:08
 * @LastEditors: MiNine
 * @LastEditTime: 2020-09-23 18:56:19
 * @Description: 
 */
import apis from '../../http/apis/apis'
import dayjs from 'dayjs';

const DEVELOP = true;

export default {
  namespaced: true,
  state: {
    adminId: "",
    pageDate: {
      total: 0,
      totalPages: 0,
      pageSize: 5,
      currentPage: 1,
      data: [],
    }
  },
  mutations: {
    setAdminId(state, val) {
      state.adminId = val;
    },
    setPageDate(state, val) {
      Object.assign(state.pageDate, val);
    },
  },
  actions: {
    /**
     * @name: 获取操作员下 所有保险订单
     * @param {type} 
     * @return {type} 
     */
    async getInsureOrders(context) {
      const {
        data
      } = await apis.insureOrderApi.getInsureOrders({
        adminId: context.state.adminId,
        pageSize: context.state.pageDate.pageSize,
        currentPage: context.state.pageDate.currentPage
      });

      if (DEVELOP) console.log(data);

      if (data.status) {
        context.commit('setPageDate', data.data);
      }
    },

    /**
     * @name: 删除保险订单
     * @param data object{_id} 订单id
     * @return object 
     */
    async delInsureOrder(context, val) {
      const {
        data
      } = await apis.insureOrderApi.delInsureOrder(val);

      if (DEVELOP) console.log(data);

      if (data.status) {
        context.dispatch('getInsureOrders');
        return true;
      } else {
        return false;
      }
    },
    /**
     * @name: 修改订单状态
     * @param {type} 
     * @return {type} 
     */
    async putInsureOrder_status(context, val) {

      const {
        _id,
        status
      } = val;

      //如果修改为理赔状态
      if (status == 6) {
        const {
          data: data1
        } = await apis.claimApi.addClaimOrder({
          insureOrderId: _id
        });

        if (DEVELOP) console.log(data1);

        if (!data1.status) {
          return false
        }
      }

      const {
        data: data2
      } = await apis.insureOrderApi.putInsureOrder_status(val);

      if (DEVELOP) console.log(data2);

      if (data2.status) {
        context.dispatch('getInsureOrders');
        return true;
      } else {
        return false;
      }
    },

    /**
     * @name: 修改订单状态
     * @param {type} 
     * @return {type} 
     */
    async putInsureOrder(context, val) {
      const {
        _id,
        value
      } = val;

      const {
        data
      } = await apis.insureOrderApi.getInsureOrder({
        _id
      });

      if (data.status) {
        const dueTime = data.data.dueTime;
        const newDueTime = dayjs(dueTime).add(value, 'day').format('YYYY-MM-DD');

        const {
          data: data2
        } = await apis.insureOrderApi.putInsureOrder({
          _id,
          updateDate: {
            dueTime: newDueTime
          }
        });

        if (DEVELOP) console.log(data2);

        if (data.status) {
          context.dispatch('getInsureOrders');
          return newDueTime;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },


    async getInsureOrder(context, val) {
      const {
        data
      } = await apis.insureOrderApi.getInsureOrder(val);

      if (DEVELOP) console.log(data);

      if (data.status) {
        return data.data;
      } else {
        return false;
      }
    }

  }
}